import api from './config'
import logger from '../utils/logger.js'

/**
 * 上传图片
 * @param {File} file - 图片文件
 * @returns {Promise} API响应
 */
export const uploadImage = async (file) => {
  try {
    logger.log('🚀 [API] 开始上传图片:', file.name)
    
    // 创建FormData
    const formData = new FormData()
    formData.append('image', file)
    
    // 使用axios上传
    const response = await api.post('/api/images/upload', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      },
      timeout: 30000 // 30秒超时，图片上传可能需要更长时间
    })
    
    logger.log('✅ [API] 图片上传响应:', response.data)
    return response
    
  } catch (error) {
    logger.error('❌ [API] 图片上传失败:', error)
    throw error
  }
}

/**
 * 删除图片
 * @param {string} imagePath - 图片路径
 * @returns {Promise} API响应
 */
export const deleteImage = async (imagePath) => {
  try {
    logger.log('🗑️ [API] 开始删除图片:', imagePath)
    
    const response = await api.delete('/api/images/delete', {
      data: { imagePath }
    })
    
    logger.log('✅ [API] 图片删除响应:', response.data)
    return response
    
  } catch (error) {
    logger.error('❌ [API] 图片删除失败:', error)
    throw error
  }
}

export default {
  uploadImage,
  deleteImage
} 